<?php

/**
 * 微信配置
 */

namespace app\admin\controller;

use think\Lang;
use think\Validate;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class  O2oOrderBill extends AdminControl {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/o2o_order_bill.lang.php');
    }

    public function index() {
        $o2o_order_bill_model = model('o2o_order_bill');
        $condition = array();

        $o2o_order_bill_validate = validate('o2o_order_bill');
        $condition['store_id'] = 0;


        $bill_state = input('bill_state');
        if ($bill_state !== '' && $bill_state !== null) {
            $condition['o2o_order_bill_state'] = $bill_state;
        }
        $query_start_date = input('param.query_start_date');
        $query_end_date = input('param.query_end_date');
        $if_start_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_date);
        $if_end_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_date);
        $start_unixtime = $if_start_date ? strtotime($query_start_date) : null;
        $end_unixtime = $if_end_date ? strtotime($query_end_date) : null;
        if ($start_unixtime) {
            $condition['o2o_order_bill_start_time'] = array('>=', $start_unixtime);
        }
        if ($end_unixtime) {
            $condition['o2o_order_bill_end_time'] = array('<=', $end_unixtime);
        }
        if (!$o2o_order_bill_validate->scene('o2o_order_bill_search')->check($condition)) {
            $this->error($o2o_order_bill_validate->getError());
        }

        $bill_list = $o2o_order_bill_model->getO2oOrderBillList($condition, '*', 12);
        $this->assign('bill_list', $bill_list);
        $this->assign('show_page', $o2o_order_bill_model->page_info->render());

        return $this->fetch();
    }
    
    
    /**
     * 查看结算单详细
     *
     */
    public function show_bill() {
        $bill_id = intval(input('param.bill_id'));
        if (!$bill_id) {
            $this->error('参数错误');
        }

        $o2o_order_bill_model = model('o2o_order_bill');
        $bill_info = $o2o_order_bill_model->getO2oOrderBillInfo(array('o2o_order_bill_id' => $bill_id, 'store_id' => 0));
        if (!$bill_info) {
            $this->error(lang('o2o_order_bill_not_exists'));
        }

        $order_condition = array();
        $order_condition['o2o_order_bill_id'] = $bill_id;
        $query_order_no = trim(input('get.query_order_no'));

        $query_start_date = input('get.query_start_date');
        $query_end_date = input('get.query_end_date');
        $if_start_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_date);
        $if_end_date = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_date);
        $start_unixtime = $if_start_date ? strtotime($query_start_date) : null;
        $end_unixtime = $if_end_date ? (strtotime($query_end_date)+86399) : null;


        $query_type = input('param.query_type');
        if ($query_type == 'errand') {
        if ($query_order_no) {
            $order_condition['o2o_errand_order_sn'] = $query_order_no;
        }
        if ($if_start_date && $if_end_date) {
            $order_condition['o2o_errand_order_finish_time'] = array('between', array($start_unixtime, $end_unixtime));
        }else if($if_start_date){
            $order_condition['o2o_errand_order_finish_time'] = array('>=',$start_unixtime);
        }else if($if_end_date){
            $order_condition['o2o_errand_order_finish_time'] = array('<=',$query_end_date);
        }
        $order_model = model('o2o_errand_order');
        $o2o_complaint_model = model('o2o_complaint');
        $order_list = $order_model->getO2oErrandOrderList($order_condition, '*', 20);
        foreach ($order_list as $key => $val) {
            $order_list[$key]['o2o_errand_order_state_text']= $order_model->getO2oErrandOrderStateText($val['o2o_errand_order_state']);
            $order_list[$key]['order_is_complaint'] = $o2o_complaint_model->getO2oComplaintInfo(array('order_id' => $val['o2o_errand_order_id'],'o2o_complaint_order_type'=>1), 'order_id');
        }
        $this->assign('show_page', $order_model->page_info);
        $this->assign('order_list', $order_list);
        $sub_tpl_name = 'show_errand_list';
        }else{
        if ($query_order_no) {
            $order_condition['order_sn'] = $query_order_no;
        }
        if ($if_start_date && $if_end_date) {
            $order_condition['finnshed_time'] = array('between', array($start_unixtime, $end_unixtime));
        }else if($if_start_date){
            $order_condition['finnshed_time'] = array('>=',$start_unixtime);
        }else if($if_end_date){
            $order_condition['finnshed_time'] = array('<=',$query_end_date);
        }
        $order_model = model('order');
        $o2o_complaint_model = model('o2o_complaint');
        $order_list = $order_model->getOrderList($order_condition, 20, 'order_sn,order_id,shipping_fee,order_state,buyer_name,order_amount,add_time,finnshed_time');
        foreach ($order_list as $key => $val) {
            $order_list[$key]['order_is_complaint'] = $o2o_complaint_model->getO2oComplaintInfo(array('order_id' => $val['order_id'],'o2o_complaint_order_type'=>0), 'order_id');
        }
        $this->assign('show_page', $order_model->page_info);
        $this->assign('order_list', $order_list);
        $sub_tpl_name = 'show_order_list';
        }
        $this->setAdminCurItem('');
        $this->assign('bill_info', $bill_info);

        return $this->fetch($sub_tpl_name);
    }


    /**
     * 平台确认出账单
     *
     */
    public function confirm_bill() {
        $bill_id = input('param.bill_id');
        if (!$bill_id) {
            $this->error(lang('param_error'));
        }
        $o2o_order_bill_model = model('o2o_order_bill');
        $condition = array();
        $condition['o2o_order_bill_id'] = $bill_id;
        $condition['store_id'] = 0;
        $condition['o2o_order_bill_state'] = O2O_ORDER_BILL_STATE_CHECK;
        $bill_info = $o2o_order_bill_model->getO2oOrderBillInfo($condition);
        if (!$bill_info) {
            $this->error(lang('o2o_order_bill_not_exists'));
        }
        if (request()->isPost()) {
            $update = $o2o_order_bill_model->editO2oOrderBill(array('o2o_order_bill_state' => O2O_ORDER_BILL_STATE_PAY, 'o2o_order_bill_remark' => input('post.ob_admin_content')), $condition);
            if ($update) {
                $this->success(lang('ds_common_op_succ'));
            } else {
                $this->error(lang('ds_common_op_fail'));
            }
        } else {

            return $this->fetch('bill_confirm');
        }
    }
    
    /**
     * 平台付款账单
     *
     */
    public function pay_bill() {
        $bill_id = input('param.bill_id');
        if (!$bill_id) {
            $this->error(lang('param_error'));
        }
        $o2o_order_bill_model = model('o2o_order_bill');
        $condition = array();
        $condition['o2o_order_bill_id'] = $bill_id;
        $condition['store_id'] = 0;
        $condition['o2o_order_bill_state'] = O2O_ORDER_BILL_STATE_PAY;
        $bill_info = $o2o_order_bill_model->getO2oOrderBillInfo($condition);
        if (!$bill_info) {
            $this->error(lang('o2o_order_bill_not_exists'));
        }
        if (request()->isPost()) {
            $o2o_order_bill_payment_voucher='';
            if (!empty($_FILES['file']['name'])) {
            $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_STORE . DS . $bill_info['store_id'] . DS . 'o2o_order_bill';
            $file = request()->file('file');
            $result = $file->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
            if ($result) {
                $o2o_order_bill_payment_voucher = $result->getFilename();
            }else{
                $this->error($file->getError());
            }
            }
            $update = $o2o_order_bill_model->editO2oOrderBill(array('o2o_order_bill_payment_voucher'=>$o2o_order_bill_payment_voucher,'o2o_order_bill_state' => O2O_ORDER_BILL_STATE_SUCCESS), $condition);
            if ($update) {
                $this->success(lang('ds_common_op_succ'));
            } else {
                $this->error(lang('ds_common_op_fail'));
            }
        } else {

            return $this->fetch('bill_pay');
        }
    }
    
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_list'),
                'url' => url('O2oOrderBill/index')
            ),
        );
        return $menu_array;
    }

}
